package chxy.sys.controller;

import chxy.sys.model.ResultBean;
import chxy.sys.model.Users;
import chxy.sys.service.UsersService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author SR.Y
 * @since 2021-03-13
 */
@Controller
@RequestMapping("/sys/users")
public class UsersController {

    @Autowired
    private UsersService usersService;

    /**
     * users登录
     */

    @PostMapping(value = "/userLogin")
    @ResponseBody
    public void login(String uname, String pwd, Model model, HttpServletResponse response){
        //获取用户信息
        response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Users u = new Users();
        u.setUname(uname);
        u.setPwd(pwd);
        ResultBean rb = new ResultBean();

        List<Users> user=usersService.findByNP(u);
        if(user.size()>0){
            rb.setFlag(true);
            rb.setMsg("登录成功");
            rb.setList(user);
        }else {
            rb.setFlag(false);
            rb.setMsg("用户名或密码错误");
        }
        try {
            PrintWriter out=response.getWriter();
            String json=JSON.toJSONStringWithDateFormat(rb, "yyyy-MM-dd");
            System.out.println(json);
            out.print(json);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    /**
     * 通过id获取用户信息
     */
    @PostMapping(value = "/getUserinfo/{id}")
    @ResponseBody
    public Users login(@PathVariable int id,Users u,HttpServletResponse response) {
        response.setContentType("application/json;charset=utf-8");
        ResultBean rb2 = new ResultBean();
        u = usersService.getById(id);
        System.out.println(u.getUname()+"====="+u.getPwd());
        return u;


    }


    /**
     * users生成
     */
    @RequestMapping(value = "/createUsers", method = RequestMethod.POST)
    @ResponseBody
    public void createUsers(@RequestBody Users u) {

        usersService.save(u);
    }

    /**
     * users修改
     */
    @RequestMapping(value = "/updateUsers/{id}", method = RequestMethod.POST)
    @ResponseBody
    public boolean updateUsers(@PathVariable int id, @RequestBody Users u) {
        u.setId(id);
        return usersService.update(u);
    }

    /**
     * users删除
     */
    @RequestMapping(value = "/deleteUsers/{id}", method = RequestMethod.POST)
    @ResponseBody
    public boolean deleteUsers(@PathVariable int id, @RequestBody Users u) {
        u.setId(id);
        return usersService.delete(u);
    }


    /**
     * users查询
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Page<Users> list(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        System.out.println("进入了users查询");

        return usersService.list(pageSize, pageNum);
    }

}

